<?php


namespace app\service;


use app\dao\RoomPasswordDao;
use app\lib\exception\ServiceException;
use app\utils\DateTime;
use app\utils\PasswordConvert;

class RoomPasswordService
{
    private $RoomPasswordDao;
    private $DateTime;

    public function __construct()
    {
        $this->DateTime = new DateTime();
        $this->RoomPasswordDao = new RoomPasswordDao();
    }

    public function getRoomPasswordByRoomId($room_id){
        return $this->RoomPasswordDao
            ->customQuery()
            ->filterByRoomId($room_id)
            ->getAll();
    }

    public function getRoomPassword($id){
        return $this->RoomPasswordDao->customQuery()->filterById($id)->withRoom()->getOne();
    }

    public function getRoomPasswordList($page , $limit){
        return $this->RoomPasswordDao->customQuery()->withRoom()->orderByRoomId()->getList($page , $limit);
    }

    // Todo ：废弃
    public function getRoomPasswordByTime($start_time , $end_time , $room_id){
        $start_time = $this->DateTime->setTime($start_time);
        $end_time = $this->DateTime->setTime($end_time);
        return $this->RoomPasswordDao->customQuery()->remove()->filterByRoomId($room_id)->StartTimeAndEndTimeByTime($start_time , $end_time)->getAll();
    }

    public function getRoomPasswordStartBetweenEndTime($room_id , $time , $start_time , $end_time){
        $start_time = $this->DateTime->setTime($start_time);
        $end_time = $this->DateTime->setTime($end_time);
        return $this->RoomPasswordDao->customQuery()->remove()->filterByRoomId($room_id)->StartBetweenEndTime($time , $start_time , $end_time)->getOne();
    }

    public function editRoomPasswordByRoomId($id , $data){
        $editData = [];
        $DateTime = new DateTime();
        $PasswordConvert = new PasswordConvert();
        empty($data['room_id']) || $editData['room_id'] = $data['room_id'];
        empty($data['password']) || $editData['password'] = $PasswordConvert->encryption($data['password']);
        empty($data['start_time']) || $editData['start_time'] = $DateTime->setTime($data['start_time']);
        empty($data['end_time']) || $editData['end_time'] = $DateTime->setTime($data['end_time']);

        return $this->RoomPasswordDao->edit($id , $editData);
    }

    public function delRoomPasswordByRoomId($room_id){
        $models = $this->RoomPasswordDao
            ->customQuery()
            ->filterByRoomId($room_id)
            ->getAll();

        foreach ($models as $model){
            $this->delRoomPassword($model->id);
        }
    }

    public function delRoomPassword($id){
        $this->RoomPasswordDao->delete($id);
    }

    public function addRoomPassword($room_id , $password , $start_time , $end_time){
        $id = (new RoomService())->getRoom($room_id);
        if ($id == null)throw new ServiceException("房间不存在");
        return $this->RoomPasswordDao->save([
            'room_id' => $id->id,
            'password' => $password,
            'start_time' => $start_time,
            'end_time' => $end_time,
        ]);
    }
}